﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_Material_Create
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then


        End If
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            'Kiem tra trung tieu de
            Dim Title As String = txtTitle.Text.Trim()
            Dim pCheck = (From qCheck In data.Materials Where qCheck.Title = Title Select qCheck).ToList()
            If pCheck.Count > 0 Then
                Excute_Javascript("Alertbox('Tiêu đề nguyên liệu này đã tồn tại, vui lòng nhập tiêu đề nguyên liệu khác.');", Me.Page, True)
            Else
                Dim p As New VSModel.Material
                Try
                    p.Title = txtTitle.Text.Trim()
                    p.QueryString = makeURLFriendly(txtTitle.Text.Trim())
                    data.Materials.AddObject(p)
                    data.SaveChanges()
                    Insert_App_Log("Create Material:" & txtTitle.Text.Trim & "", Function_Name.Material, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Material/List.aspx';", Me.Page, True)
                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
                End Try
            End If
        End Using
    End Sub
#End Region

    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
End Class
